home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / FPSE_src / system / amiga / plugin / gpu / wingpu.c < prev   
Encoding:
C/C++ Source or Header  |  2000-01-01  |  8.7 KB  |  367 lines

  1. #include "gpulocal.h"
  2.  
  3. #include <exec/types.h>
  4. #include <exec/nodes.h>
  5. #include <exec/lists.h>
  6. #include <exec/memory.h>
  7.  
  8. #include <powerup/ppcproto/intuition.h>
  9. #include <powerup/ppclib/interface.h>
  10. #include <powerup/ppclib/time.h>
  11. #include <powerup/gcclib/powerup_protos.h>
  12. #include <powerup/ppcproto/exec.h>
  13. #include <powerup/ppcinline/graphics.h>
  14. #include <powerup/ppcproto/dos.h>
  15. #include <powerup/ppcproto/asl.h>
  16.  
  17. #include <cybergraphx/cybergraphics.h>
  18. #include <powerup/ppcinline/cybergraphics.h>
  19.  
  20. #define DEF_W 256
  21. #define DEF_H 240
  22.  
  23. /* Misc variables */
  24. struct IntuitionBase *IntuitionBase;
  25. struct GfxBase *GfxBase;
  26. struct Library *CyberGfxBase;
  27. struct Window *window = NULL;
  28. struct Screen *screen = NULL;
  29. int libraries_is_open = 0;
  30. char *disp_buf = NULL, *true_buf = NULL;
  31. int amiga_width, amiga_height, amiga_depth;
  32. int cur_w = DEF_W, cur_h = DEF_H;
  33.  
  34. extern int amiga_use_screen;
  35. int joy0_ret = 0xffff;
  36. int joy1_ret = 0xffff;
  37.  
  38. /* CGFX variables */
  39. unsigned long pixfmt;
  40. unsigned char *baseaddress;
  41. int bpr;
  42.  
  43. void open_libraries() {
  44.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39);
  45.   if (IntuitionBase == NULL) {
  46.     printf("Could not open intuition.library v39 or later!\n");
  47.     exit(0);
  48.   }
  49.  
  50.   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39);
  51.   if (GfxBase == NULL) {
  52.     CloseLibrary(IntuitionBase);
  53.     printf("Could not open graphics.library v39 or later!\n");
  54.     exit(0);
  55.   }
  56.  
  57.   CyberGfxBase = OpenLibrary("cybergraphics.library", 40);
  58.   if (CyberGfxBase == NULL) {
  59.     CloseLibrary(GfxBase);
  60.     CloseLibrary(IntuitionBase);
  61.     printf("Could not open cybergraphics.library v40 or later!\n");
  62.     exit(0);
  63.   }
  64.  
  65.   libraries_is_open = 1;
  66. }
  67.  
  68. void close_libraries() {
  69.   CloseLibrary(CyberGfxBase);
  70.   CloseLibrary(GfxBase);
  71.   CloseLibrary(IntuitionBase);
  72. }
  73.  
  74. void amiga_exit() {
  75.   if (window != NULL) {
  76.     CloseWindow(window);
  77.     window = NULL;
  78.   }
  79.   if (screen != NULL) {
  80.     CloseScreen(screen);
  81.     screen = NULL;
  82.   }
  83.   if (libraries_is_open) {
  84.     close_libraries();
  85.     libraries_is_open = 0;
  86.   }
  87. }
  88.  
  89. void write_shot_ppm()
  90. {
  91.   static int serial = 0;
  92.  
  93.   unsigned char *buf, *dst;
  94.   unsigned short *src;
  95.   FILE *fp = NULL;
  96.   char filename[32];
  97.  
  98.   buf = malloc(cur_w*cur_h*3);
  99.   if (buf != NULL) {
  100.     int x, y;
  101.  
  102.     src = (unsigned short*)(disp_buf+(gpu.disp.x*2)+(gpu.disp.y*1024*2));
  103.     dst = buf;
  104.  
  105.     for (y=0; y<cur_h; y++) {
  106.       for (x=0; x<cur_w; x++) {
  107.         unsigned short val = *src++;
  108.         *dst++ = ((val & 0x7c00) >> 7);
  109.         *dst++ = ((val & 0x03e0) >> 2);
  110.         *dst++ = ((val & 0x001f) << 3);
  111.       }
  112.       src+=(1024-cur_w);
  113.     }
  114.   } else {
  115.     return;
  116.   }
  117.  
  118.   sprintf(filename, "shot%04d.ppm", serial);
  119.   fp = fopen(filename, "w");
  120.   if (fp != NULL) {
  121.     // Write PPM header
  122.     fprintf(fp, "P6\n%d %d\n255\n", cur_w, cur_h);
  123.     // Write PPM data (24bit)
  124.     fwrite(buf, cur_w*cur_h*3, 1, fp);
  125.     fclose(fp);
  126.   }
  127.  
  128.   free(buf);
  129.   serial++;
  130. }
  131.  
  132. void win_resize(int w,int h)
  133. {
  134.   if (amiga_use_screen) {
  135.     if ((w == cur_w) && (h == cur_h)) {
  136.       return;
  137.     }
  138.  
  139.     cur_w = w;
  140.     cur_h = h;
  141.  
  142.     /* Clear the screen */
  143.     FillPixelArray(&screen->RastPort, 0, 0, amiga_width, amiga_height, 0x00000000);
  144.     return;
  145.   }
  146.  
  147.   if (true_buf == NULL) {
  148.     true_buf = malloc(w*h*4);
  149.   }
  150.  
  151.   if ((w == cur_w) && (h == cur_h)) {
  152.     return;
  153.   }
  154.  
  155.   cur_w = w;
  156.   cur_h = h;
  157.  
  158.   free(true_buf);
  159.   true_buf = malloc(cur_w*cur_h*4);
  160.  
  161.   ChangeWindowBox(window, 100, 100, window->BorderLeft+cur_w+window->BorderRight, window->BorderTop+cur_h+window->BorderBottom);
  162. }
  163.  
  164. static int keybind[16] = {
  165.      2,  /* '2' L2 */
  166.      10, /* '0' R2 */
  167.      1,  /* '1' L1 */
  168.      9,  /* '9' R1 */
  169.      52, /* 'V' /\ */
  170.      49, /* 'Z'  O */
  171.      50, /* 'X'  X */
  172.      51, /* 'C' [] */
  173.      66, /* SELECT (TAB) */
  174.      0,
  175.      0,
  176.      68, /* START (ENTER) */
  177.      76, /* UP */
  178.      78, /* RIGHT */
  179.      77, /* DOWN */
  180.      79  /* LEFT */
  181. };
  182.  
  183. void screen_update()
  184. {
  185.   struct IntuiMessage *imsg = NULL;
  186.   ULONG imCode,imClass;
  187.   int i;
  188.  
  189.   /* Check for IDCMP messages */
  190.   if ((imsg = (struct IntuiMessage *)GetMsg(window->UserPort)))
  191.   {
  192.     imClass = imsg->Class;
  193.     imCode = imsg->Code;
  194.  
  195.     ReplyMsg((struct Message *)imsg);
  196.  
  197.     if (imClass == IDCMP_REFRESHWINDOW)
  198.     {
  199.       BeginRefresh(window);
  200.       EndRefresh(window, TRUE);
  201.     }
  202.     else if (imClass == IDCMP_CLOSEWINDOW)
  203.     {
  204.       exit(0);
  205.     }
  206.     else if (imClass == IDCMP_RAWKEY)
  207.     {
  208.       if (imCode == 69) {
  209.         FPSE_Flags |= STOP;
  210.       }
  211.  
  212.       if (imCode == 0xdf) {
  213.         write_shot_ppm();
  214.       }
  215.  
  216.       for(i=0;i<16;i++) {
  217.         int keycode = keybind[i];
  218.         if (keycode == (imCode & 0x7f)) {
  219.           if (imCode < 128) {
  220.             joy0_ret &= ~(1<<i);
  221.           } else {
  222.             joy0_ret |= (1<<i);
  223.           }
  224.         }
  225.       }
  226.     }
  227.   }
  228.  
  229.   if (amiga_use_screen) {
  230.     unsigned char *src, *dst;
  231.     int y;
  232.  
  233.     dst = baseaddress+((amiga_height-cur_h)/2)*bpr+((amiga_width-cur_w)/2)*2;
  234.     src = disp_buf+(gpu.disp.x*2)+(gpu.disp.y*1024*2);
  235.  
  236.     for (y=0; y<cur_h; y++) {
  237.       memcpy(dst, src, cur_w*2);
  238.       dst += bpr;
  239.       src += 1024*2;
  240.     }
  241.   } else {
  242.     unsigned long *dst;
  243.     unsigned short *src;
  244.     int x, y;
  245.  
  246.     dst = (unsigned long*)true_buf;
  247.     src = (unsigned short*)(disp_buf+(gpu.disp.x*2)+(gpu.disp.y*1024*2));
  248.  
  249.     for (y=0; y<cur_h; y++) {
  250.       for (x=0; x<cur_w; x++) {
  251.         unsigned short val = *src++;
  252.         *dst++ = ((val & 0x7c00) << 9) | ((val & 0x03e0) << 6) | ((val & 0x1f) << 3);
  253.       }
  254.       src+=(1024-cur_w);
  255.     }
  256.     WritePixelArray(true_buf, 0, 0, cur_w*4, window->RPort, window->BorderLeft, window->BorderTop, cur_w, cur_h, RECTFMT_ARGB);
  257.   }
  258. }
  259.  
  260. void *win_createbitmap(int w,int h,int bits)
  261. {
  262.   open_libraries();
  263.  
  264.   if (amiga_use_screen) {
  265.     unsigned long VideoMode;
  266.     VideoMode = BestCModeIDTags(CYBRBIDTG_NominalWidth, 640,
  267.                                 CYBRBIDTG_NominalHeight, 480,
  268.                                 CYBRBIDTG_Depth, 15,
  269.                                 TAG_DONE);
  270.  
  271.     if (VideoMode == INVALID_ID) {
  272.       printf("BestCModeID failed, no usefull screenmode available!\n");
  273.       close_libraries();
  274.       exit(0);
  275.     }
  276.  
  277.     /* Get VideoMode information */
  278.     amiga_width = GetCyberIDAttr(CYBRIDATTR_WIDTH, VideoMode);
  279.     amiga_height = GetCyberIDAttr(CYBRIDATTR_HEIGHT, VideoMode);
  280.     amiga_depth = GetCyberIDAttr(CYBRIDATTR_DEPTH, VideoMode);
  281.  
  282.     if ((amiga_width != 640) || (amiga_height != 480) || (amiga_depth != 15)) {
  283.       printf("BestCModeID failed, no usefull screenmode available!\n");
  284.       close_libraries();
  285.       exit(0);
  286.     }
  287.  
  288.     /* Open screen */
  289.     screen = OpenScreenTags(NULL,
  290.              SA_Width,amiga_width,
  291.              SA_Height,amiga_height,
  292.              SA_Depth,amiga_depth,
  293.              SA_Quiet,TRUE,
  294.              SA_ShowTitle,FALSE,
  295.              SA_Type,CUSTOMSCREEN,
  296.              SA_DisplayID,VideoMode,
  297.              TAG_DONE);
  298.  
  299.     /* Could the screen be opened? */
  300.     if (screen == NULL) {
  301.       close_libraries();
  302.       printf("Could NOT open screen!\n");
  303.       exit(0);
  304.     }
  305.  
  306.     /* Open window */
  307.     window = OpenWindowTags(NULL,
  308.              WA_CustomScreen,(int)screen,
  309.              WA_Width,screen->Width,
  310.              WA_Height,screen->Height,
  311.              WA_IDCMP,IDCMP_RAWKEY,
  312.              WA_Backdrop,TRUE,
  313.              WA_Borderless,TRUE,
  314.              WA_Activate,TRUE,
  315.              TAG_DONE);
  316.  
  317.     /* Could the window be opened? */
  318.     if (window == NULL) {
  319.       CloseScreen(screen);
  320.       close_libraries();
  321.       printf("Could NOT open window!\n");
  322.       exit(0);
  323.     }
  324.  
  325.     /* Get screenmode information */
  326.     UnLockBitMap(LockBitMapTags(screen->RastPort.BitMap,
  327.                                 LBMI_PIXFMT,(ULONG)&pixfmt,
  328.                                 LBMI_BASEADDRESS,(ULONG)&baseaddress,
  329.                                 LBMI_BYTESPERROW,(ULONG)&bpr,
  330.                                 TAG_END));
  331.  
  332.     /* Clear the screen */
  333.     FillPixelArray(&screen->RastPort, 0, 0, amiga_width, amiga_height, 0x00000000);
  334.   } else {
  335.     window = OpenWindowTags(NULL,
  336.                             WA_Title,"FPSE 0.08, Amiga revision 2 (000805)",
  337.                             WA_Flags,WFLG_SIMPLE_REFRESH|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_RMBTRAP,
  338.                             WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY,
  339.                             WA_Left,100,
  340.                             WA_Top,100,
  341.                             WA_InnerWidth,DEF_W,
  342.                             WA_InnerHeight,DEF_H,
  343.                             TAG_DONE);
  344.  
  345.     if (window == NULL) {
  346.       close_libraries();
  347.       exit(0);
  348.     }
  349.   }
  350.  
  351.   disp_buf = (char *)malloc(w*h*(bits/8));
  352.   if (disp_buf == NULL) {
  353.     close_libraries();
  354.     exit(0);
  355.   }
  356.  
  357.   memset(disp_buf, 0, (w*h*(bits/8)));
  358.  
  359.   atexit(amiga_exit);
  360.  
  361.   return (void *)disp_buf;
  362. }
  363.  
  364. void win_destroy(void)
  365. {
  366.   /* Do nothing */
  367. }